<?php

namespace app\admin\controller;

use app\admin\model\merchant\MerchantModel;
use app\admin\model\merchant\MerchantMoneyLog;
use app\Request;
use think\exception\ValidateException;
use think\facade\Session;
use ulit\CryptAES;

class Merchant extends Base
{



    public function list(){



        return view('merchant/list');
    }

    public function ajaxlist(): \think\response\Json
    {


        $result=MerchantModel::merchantList();

        return $this->success($result);

    }


    public function add(Request $request){

        if($request->isPost() && $request->isAjax()){

            $result= MerchantModel::add();
            return $this->result($result);
        }

        $info=MerchantModel::where(['admin_id'=>session('admin_id')])->find() ;

        $info? $tpl='edit':$tpl='add';

        return view($tpl,['info'=>$info]);
    }

    public function edit(Request $request){

        if($request->isPost() && $request->isAjax()){

            $id= input('id');

            $result= MerchantModel::edit($id);
            return $this->result($result);
        }
        $info=MerchantModel::where(['id'=>input('id')])->find() ;
        return view('',['info'=>$info]);
    }


    public function merchantDel(){

        $res=MerchantModel::where(['id'=>input('id')])->delete();

        if($res){
            return $this->success();
        }else{
            return $this->error();
        }

    }

    public function updateStatus(){

        $id= input('id');

        $result= MerchantModel::where(['id'=>$id])->update(['status'=>input('status')]);



        return $this->success();
    }

    public function balance(Request $request){


        $info=MerchantModel::where(['id'=>input('id')])->find();

        if($request->isPost() && $request->isAjax()){

            if(input('case')=='add'){
                MerchantModel::where(['id'=>input('id')])->inc('money',input('money'))->update();
            }else{
                MerchantModel::where(['id'=>input('id')])->dec('money',input('money'))->update();
            }

            $add=input('');
            $add['operate_id']=Session::get('admin_id');
            $add['balance']=$info->money;
            MerchantMoneyLog::log($add);

            return $this->success();
        }

        return view('merchant/balance',['info'=>$info]);
    }

    public function balanceLog(Request $request)
    {
        if ($request->isGet() && $request->isAjax()) {
            $list=MerchantMoneyLog::Order(['id desc'])->select();

            foreach ($list as $key => $value) {
                $list[$key]['supr_money']=$value->balance+$value->money;
            }

            return $this->success($list);
        }

        return view('merchant/balance_log');
    }

    public function modifyPassword(Request $request ): \think\response\View|\think\response\Json
    {
        $info=MerchantModel::where(['id'=>input('id')])->find();
        if($request->isPost() && $request->isAjax()){
            $aes=new CryptAES();
            try {
                $add=input('');
                $result = validate(\app\admin\validate\merchant\Merchant::class)->scene('ModifyPassword')->check($add);
                if(!empty($result)){
                    $password=$aes->aesEncode(input('password'));
                     MerchantModel::where(['id'=>input('id')])->update(['password'=>$password]);
                    return $this->success();
                    // dump($result);die;
                }
            } catch (ValidateException $e) {
                // 验证失败 输出错误信息
                return $this->result($e->getError());
            }
        }
        return view('merchant/modify_password',['info'=>$info]);
    }



}